load("js/keys_comm.js");
load('js/v6/abc/abc_common.js');
var billDate = null;
/**
 * 抓取请求-响应信息
 * @type {{crawlBasicInfoData: crawlReqData.crawlBasicInfoData}}
 */
var crawlReqData = {
    /**
     * 抓取用户卡列表信息，判断是否有卡，如果没有停止抓取
     * @returns {*}
     */
    crawlFashionCardList:function () {
        try {
            var indexHtml = context.get("fashion_basic_card_list");
            var cardInfo = RegexUtils.GetText(indexHtml, " var cardInfo = JSON.parse\\(\\'*(.*)").replaceAll(";", "").replaceAll("\'", "").replaceAll("\\)", "");
            var listJsonObject = null;
            try {
                listJsonObject = JSON.toJavaObject(JSON.parseArray(cardInfo), List.class);
            } catch (e) {
                // TODO: handle exception
                logger.info("abcbank taskKey = {}  解析当前页面inddex.txt失败,错误原因{}",context.get(LUA_TASK_KEY),e);
            }
            if (listJsonObject == null || listJsonObject.size() == 0) {
                logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY) + "获取账户信息失败，停止抓取");
                return;
            }
            //获取卡列表
            // var card_num = null;
            var debitCardList=new ArrayList();
            var creditCardList=new ArrayList();
            for(var i=0;i<listJsonObject.size();i++){
                var jsonObject = listJsonObject.get(i);
                var card_type = jsonObject.getString("card_type");
                if (equals("1",card_type)) {
                    var debit_card_num = jsonObject.getString("card_no");//卡号
                    if (debit_card_num.length() > 0) {
                        debitCardList.add(debit_card_num);
                    }
                }else if (equals("2",card_type)) {
                    var credit_card_num = jsonObject.getString("card_no");//卡号
                    if (credit_card_num.length() > 0) {
                        creditCardList.add(credit_card_num);
                    }
                }else{
                    logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY) + "获取到其他卡信息，暂不抓取");
                }

            }
            context.put(keys.DEBT_CARD_LIST,debitCardList)
            context.put(keys.CRED_CARD_LIST,creditCardList)
            context.put(keys.DEBT_CARD_SIZE,debitCardList.size())
            context.put(keys.CRED_CARD_SIZE,creditCardList.size())
        }catch (e) {
            logger.info("农业 taskKye= {} 抓取首页卡列表，报错，错误信息：{}",context.get(LUA_TASK_KEY),e);
        }
    },
    /**
     * 抓取用户卡列表信息
     */
    crawlFashionAccountCardInfo:function () {
        var basicLsit = null;
        try {
            var deposit_cardInfoHtml = context.get("deposit_cardInfo");
            var split = deposit_cardInfoHtml.split("new AccViewModel(");
            var split2 = split[1].split("));");
            //String info=split2[0].substring(split2[0].indexOf("{"), split2[0].indexOf("}")+1);
            var info=split2[0].trim().substring(1, split2[0].indexOf("}]',")).trim();
            info=info.substring(0, info.length()-2);
            //JSONArray array = (JSONArray) JSONArray.parse(info);
            basicLsit = JSON.toJavaObject(JSON.parseArray(info), List.class);
        }catch (e) {
            logger.error("农业 taskKye= {} 借记卡账户基本信息解析失败，停止抓取,错误原因为{}",context.get(LUA_TASK_KEY),e);
        }
        return basicLsit;
    },
    /**
     * 保存卡基本信息
     */
    saveCreditCardAllData:function (creditCardAllData,basicInfoJsonObject,full_card_num,cardType) {
        var uuid = UUID.randomUUID().toString()
        logger.info("uuid:" + uuid);
        creditCardAllData.setBankName("农业银行");
        creditCardAllData.setCardId(UUID.randomUUID().toString());
        creditCardAllData.setCashBalance(null);
        creditCardAllData.setCashLimit(null);
        creditCardAllData.setCreditLimit(null);
        if(equals("DEBIT",cardType)){
            var cardOnName = basicInfoJsonObject.getString("acctName");
            context.put("cardOnName",cardOnName)
            creditCardAllData.setNameOnCard(basicInfoJsonObject.getString("acctName"));
            creditCardAllData.setOpenBank(basicInfoJsonObject.getString("branchName"));
            creditCardAllData.setCardType("借记卡");
        }else {
            creditCardAllData.setNameOnCard(context.get("cardOnName"));
            creditCardAllData.setCardType("信用卡");
        }
        creditCardAllData.setFullCardNum( full_card_num);
        if (full_card_num!==null &&full_card_num.length()>4) {
            creditCardAllData.setCardNum(full_card_num.substring(full_card_num.length()-4,full_card_num.length()));
        }
    },
    /**
     * 抓取卡存款信息
     */
    carwlFashionDepsoitInfo:function (creditCardAllData,depositCardNum) {
        var balance_json = context.get(depositCardNum +"_balance_json");
        var deposit_cardInfo = context.get(depositCardNum +"_deposit_cardInfo");
        try {
            var list=JSON.toJavaObject(JSON.parseArray(balance_json), List.class);
            if (list!==null&&list.size()>0) {
                for(var i=0;i<list.size();i++){
                    var listJson = list.get(i);
                    var balance = listJson.getString("currBalance");
                    creditCardAllData.setBalance(stringToBigDecimal(balance));
                }
            }
        } catch (e) {
            logger.info("解析当前借记卡号{}的账户额度信息失败,错误信息为",depositCardNum,e);
        }
        var accJsonArray=null;
        try {
            // {"errorCode":"4401","userErrorMsg":"转出方账户信息有误"}
            accJsonArray = JSON.parseObject(deposit_cardInfo).getJSONArray("table");
            var deposit_cardInfo_json = JSON.parseObject(deposit_cardInfo);
            var errorCode = deposit_cardInfo_json.get("errorCode");
            if(!equals("4401",errorCode)){
                for(var i=0;i<accJsonArray.size();i++){
                    var accJsonObject = accJsonArray.getJSONObject(i);
                    logger.info("解析当前卡借记卡的存款信息accJsonObject:{}",accJsonObject);
                    var deposit=new CreditCardTimeDeposit();
                    try {
                        deposit.setBalance(stringToBigDecimal(accJsonObject.getString("余额")));
                        logger.info("当前第{}卡的存款币种为{}",depositCardNum + 1,accJsonObject.getString("币种名"));
                        deposit.setCurrencyType(CreditResultUtil.convertCurr(accJsonObject.getString("币种名")));
                        deposit.setDepositDate(getDate(accJsonObject.getString("开户日期处理"),"yyyy-MM-dd"));
                        deposit.setDepositType(accJsonObject.getString("账户类型名"));
                        var duedatessss = accJsonObject.getString("到期日期处理");//为了与可能的变量名称区别开来
                        // duedatessss=duedatessss.replaceAll("\\","")
                        if(!duedatessss.constructor("\\")){
                            deposit.setDueDate(getDate(duedatessss,"yyyy-MM-dd"));
                        }
                        var yearInsterest = stringToBigDecimal(accJsonObject.getString("年利率")).divide(stringToBigDecimal("100"));
                        var yearIns = yearInsterest.stripTrailingZeros().toPlainString();
                        // deposit.setDueDate(yyyy_MM_dd_PARSER.parse(jsonObject.getString("到期日期处理")));
                        deposit.setInterest(stringToBigDecimal(yearIns));
                        var periodInfo=accJsonObject.getString("存期处理");
                        if (periodInfo.contains("\\")) {
                            deposit.setPeriod(null);
                            deposit.setPeriodUnit("");
                            deposit.setDueDate(null);
                        }else{
                            setDepositPeriod(periodInfo,deposit);
                        }
                        creditCardAllData.getDeposits().add(deposit);
                    } catch (e) {
                        statCaughtException(context, e);
                        logger.info("abc bank task = {} 解析借记卡存款信息失败，失败原因 {}", e);
                    }

                }
            }else{
                logger.info("当前卡:{}，无存款信息，停止解析改卡的存款信息，接续抓取",depositCardNum);
            }
        } catch (e) {
            logger.error("时尚版借记卡解析存款等信息失败，错误原因为{}",e);
        }
    },
    /**
     * 解析6个月的账单
     * @param basicInfo
     * @param creditCardAllData
     * @param dateList
     */
    crawlDepsoitBillOnNewEdition:function (creditCardAllData,basicInfo,dateList) {
        var billsFilePaths = context.get(dateList[0]+"_"+dateList[1] + "billsFilePaths");
        var bills = creditCardAllData.getBills();
        try {
            var allbills = commonUtils.read2003Excel(new File(billsFilePaths));
            if (allbills == null || allbills.size() < 4) {
                logger.info("抓取借记卡" + dateList[0] + "月以及之后的六个月数据无数据，停止解析");
                return;
            }
            var billAllData=new BillAllData();
            billAllData.setBankName("农业银行");
            billAllData.setBillId(UUID.randomUUID().toString());
            billAllData.setBillType("");
            logger.info("解析借记卡" + dateList[0] + "月以及之后的六个月数据");
            //解析excel文件
            parseDepositBillExcelOnNewEdition(allbills, billAllData,bills,basicInfo.getString("acctNo"));
            //------------------无用操作，暂时注释
            var size = billAllData.getShoppingSheets().size();
            if (size == 0) {
                logger.info("解析借记卡" + dateList[0]+"至"+dateList[1] + "的六个月无数据");
            }
        } catch ( e) {
            e.printStackTrace();
            logger.info("解析借记卡" + dateList[0]+"至"+dateList[1] + "的六个月数据失败，停止解析，失败原因为{}",e);
        }
    },
    /**
     * 抓取信用卡基本信息
     * @param creditCardAllData
     */
    crawlCreditCardInfo:function (creditCardAllData,creditIndex) {
        var creditCardInfo = context.get(creditIndex +"creditCardInfo");
        var cardInfoObj =  JSON.parse(creditCardInfo);
        var jsonObject = cardInfoObj.getJSONArray("ccCardPaymentInfo").getJSONObject(0);
        var credit_limit = jsonObject.getString("dAMT_CRLM");
        var cash_limit = jsonObject.getString("dAMT_CRLM_CSH");
        billDate = jsonObject.getString("szBILL_DAY");
        var balance = jsonObject.getString("dAMT_CUMP");
        var cash_balance = jsonObject.getString("dAMT_CSH");
        creditCardAllData.setCreditLimit(stringToBigDecimal(credit_limit));
        creditCardAllData.setCashLimit(stringToBigDecimal(cash_limit));
        creditCardAllData.setBalance(stringToBigDecimal(balance));
        creditCardAllData.setCashBalance(stringToBigDecimal(cash_balance));
    },
    /**
     * 获取可查询的账单月数据
     * 数据格式：[
     *    *          {"billMonth": "20180808-20180907","billDate": "20180907"},
     *               {"billMonth": "20180708-20180807","billDate": "20180807"}
     *          ]
     * @param creditCardNum
     */
    crawlCreditDoneBills:function (creditIndex) {
        var monthListHeml = context.get(creditIndex+"_queryMonthList");
        var queryAllMonth = RegexUtils.GetText(monthListHeml, "var convertListStr =*(.*)").replaceAll(";", "");
        var list = new ArrayList();
        try {
            list = JSON.toJavaObject(JSON.parseArray(queryAllMonth), List.class);
        } catch ( e) {
            e.printStackTrace();
            logger.info("所有能查询的月份解析失败，停止抓取未出账单");
        }
        context.put(creditIndex+"queryMonthList",list)
    },

    crawlCreditQueryMonthDoneBill:function (creditCardAllData,billAllData,creditIndex,fullCardNum,billMonth,billDate) {
        var billlDateHtml = context.get(creditIndex + "_credit__bills_" + billMonth);
        var billInfoStr = RegexUtils.GetText(billlDateHtml, "var billInfoList = *(.*)").replaceAll(";", "");
        var array = JSON.parse(billInfoStr);
        billAllData.setBankName("农业银行");
        billAllData.setBillDate(getDate(billDate,"yyyyMMdd"));
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("DONE");
        billAllData.setBillMonth(getDateStr(billDate,"yyyyMMdd","yyyy-MM"));//amtPmtdAll
        billAllData.setCashLimit(null);

        billAllData.setUsdCashLimit(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setUsdCreditLimit(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setLastPayment(null);
        //此情况为json数组长度为0，代表无数据
        if (array.length == 0 || array == null) {
            logger.info("当前月{}无账单信息,停止抓取该月，继续抓取下一个月", billMonth);
            billAllData.setCreditLimit(creditCardAllData.getCreditLimit());
            billAllData.setNewCharges(null);
            billAllData.setNewBalance(null);
            billAllData.setMinPayment(null);
            if (creditCardAllData.getBills().size() >= (creditIndex - 1) && creditIndex - 1 >= 0) {
                var lastBill = creditCardAllData.getBills().get(creditIndex - 1);
                lastBill.setLastBalance(null);
            }
            return;
        } else {
            logger.info("当前月{}有账单信息，继续抓取该月", billMonth);

        }
        var billInfoJSon = array.getJSONObject(0);
        //取数据
        var pay_ment_duedate = RegexUtils.GetText(billlDateHtml, "var dueDate =*(.*)").replaceAll(";", "").replaceAll("\"", "");
        var credit_limit = billInfoJSon.getString("amtCrlm");
        var new_charges = billInfoJSon.getString("amtDbNew");
        var min_payment = billInfoJSon.getString("amtDue");
        var last_payment = billInfoJSon.getString("amtCrNew");
        var last_balance = billInfoJSon.getString("balBen");
        var new_balance = billInfoJSon.getString("amtPmtdAll");
        var currency = billInfoJSon.getString("codCcyEn");//
        billAllData.setPaymentDueDate(getDate(pay_ment_duedate,"yyyyMMDD"));
        if (currency.equals("USD")) {
            logger.info("ABC taskKey={}当前账单含有美元信息，", context.get(LUA_TASK_KEY));
            billAllData.setUsdLastBalance(stringToBigDecimal("0"));
            billAllData.setUsdLastPayment(stringToBigDecimal("-" + last_payment));
            billAllData.setUsdMinPayment(stringToBigDecimal("0"));
            billAllData.setUsdNewBalance(stringToBigDecimal(new_balance));
            billAllData.setUsdNewCharges(stringToBigDecimal(new_charges));
        }
        billAllData.setCreditLimit(stringToBigDecimal(credit_limit));
        //billAllData.setCashLimit(billAllData.getCreditLimit().divide(stringToBigDecimal("2")));
        billAllData.setNewBalance(stringToBigDecimal(new_balance));
        if (creditCardAllData.getBills().size() > 0 && creditIndex == 0) {//即有未出账单  且为第一次循环，取第一次账单的newbalance
            var billAllData2 = creditCardAllData.getBills().get(0);
            if (billAllData2.getBillType().equals("UNDONE")) {
                //当前new_balance可能为null 当上一期未出账单为空的时候可能会出现空指针
                var undoneNewBalance = billAllData2.getNewBalance();
                if (undoneNewBalance == null) {
                    undoneNewBalance = stringToBigDecimal("0");
                }
                billAllData2.setNewBalance(undoneNewBalance.add(stringToBigDecimal(new_balance)));
            }
        }
        billAllData.setLastPayment(stringToBigDecimal("-" + last_payment));
        billAllData.setLastBalance(stringToBigDecimal(last_balance));
        billAllData.setNewCharges(stringToBigDecimal(new_charges));
        billAllData.setMinPayment(stringToBigDecimal(min_payment));
        // if (creditCardAllData.getBills().size() >= (creditIndex - 1) && creditIndex - 1 >= 0) {
        //     var lastBill = creditCardAllData.getBills().get(creditIndex - 1);
        //     // var stringToBigDecimal = stringToBigDecimal(new_balance);
        //     lastBill.setLastBalance(stringToBigDecimal(new_balance));
        // }
        // var card_num = param.get("ccCardBillQry");
        logger.info("账户请求{}月具体的消费明细", billMonth);//返回为json数据格式
    },

    /**
     *  抓取信用卡账单数据明细
     * @param creditIndex
     * @param fullCardNum
     * @param billDate
     */
    crawlCreditDoneBillInfo:function (billAllData,creditIndex,fullCardNum,billDate) {
        try {
            var billDetailJson = context.get(creditIndex + "_credit_shopping_sheets" + billDate);
            var list = JSON.toJavaObject(JSON.parseArray(billDetailJson), List.class);
            if (list!=null &&list.size()>0) {
                var shopping_sheets = billAllData.getShoppingSheets();
                var order_index=shopping_sheets.size()+1;
                for(var i=0;i<list.size();i++){
                    var jsonObject = list.get(i);
                    var expense=new Expense();
                    var tranDate=jsonObject.getString("trDate");//交易
                    var recordDate = jsonObject.getString("postDate");//记账日期
                    var transRemark=jsonObject.getString("trTxt");//描述
                    var addr=jsonObject.getString("trAddress");//垫底
                    var transAmount=jsonObject.getString("postAmt");//消费金额
                    var currecyType = jsonObject.getString("postCod");//币种  01代表人民币
                    // var lastBalense = jsonObject.getString("balBen");//币种  01代表人民币
                    expense.setId(UUID.randomUUID().toString());
                    fullCardNum=fullCardNum.substring(fullCardNum.length()-4,fullCardNum.length());
                    expense.setCardNum(fullCardNum);
                    if (StringUtils.isNotBlank(recordDate)) {
                        // 正则表达式，将20171010格式转化为2017-10-10
                        recordDate = recordDate.replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-");
                    }
                    expense.setTransAddr(addr);
                    expense.setOrderIndex(order_index);
                    order_index++;
                    expense.setPostDate(getDate(recordDate,"yyyy-MM-dd"));
                    // 格式化日期
                    if (StringUtils.isNotBlank(tranDate)) {
                        tranDate = tranDate.replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-");
                    }
                    expense.setTransDate(getDate(tranDate,"yyyy-MM-dd"));// 格式化日期
                    if (StringUtils.isNotBlank(transAmount)) {
                        // 消费金额应该为正
                        expense.setAmountMoney(
                            stringToBigDecimal(transAmount).multiply(stringToBigDecimal("-1")));
                    } else {
                        expense.setAmountMoney(stringToBigDecimal("0"));
                    }
                    if (currecyType.equals("01")) {
                        expense.setCurrencyType("CNY");
                    }else if (currecyType.equals("14")) {
                        expense.setCurrencyType("USD");
                        logger.info("有美元账单信息，需要继续抓取美元");
                    }else {
                        expense.setCurrencyType("CNY" +
                            "");
                    }
                    expense.setBalance(null);
                    expense.setDescription(transRemark);
                    //使用新的消费类型判断
                    if (transAmount.contains("-")) {
                        transAmount=transAmount.replaceAll("-", "");
                    }else{
                        transAmount="-"+transAmount;
                    }
                    var category=CategoryUtil.getCreditCategory("ABC", transRemark, transAmount);
                    expense.setCategory(category);// 判断消费类型
                    shopping_sheets.add(expense);
                }
            }
        }catch (e) {
            logger.info("农业银行，任务{}，抓取已出账单数据报错；errorMsg:{}",context.get(LUA_TASK_KEY),e);
            statCaughtException(context, e);
        }
    },
    /**
     * * 抓取信用卡账单数据中分期数据详细信息
     */
    crawlCreditDoneInstallmentInfo:function (billAllData,creditIndex,card_num,billDate) {
        var installmentJson = context.get(creditIndex + "_credit_installment" + billDate);
        var list = JSON.toJavaObject(JSON.parseArray(installmentJson), List.class);
        if (list!==null &&list.size()>0) {
            var installments = new ArrayList();
            var handFeeList=new LinkedList();//分期手续费的集合
            var shoppingSheests = billAllData.getShoppingSheets();
            for(var i = 0;i<shoppingSheests.size();i++){
                var description = shoppingSheests.get(i).getDescription();
                if(description.contains("手续费")){
                    var installmentVo=new Installment();
                    installmentVo.setTransferfeeDesc("");
                    var handFee=shoppingSheests.get(i).getAmountMoney();
                    installmentVo.setHandingFee(handFee);
                    installmentVo.setHandingfeeDesc(description);
                    handFeeList.add(installmentVo);
                }
            }
            for(var i=0;i<list.size();i++){
                //[{"billCnt":"3","remainCnt":"3","insmType":"账单分期","codCcy":"01","openDate":"20180522","capAmt":"5000.00","feeAmt":"833.00"}]
                var jsonObject = JSON.parseObject(list.get(i).toString());
                var installment=new Installment();
                var installment_type = jsonObject.getString("insmType");
                var start_date = jsonObject.getString("openDate");//分期日期
                var allmoney = jsonObject.getString("capAmt");//总金额/币种
                // var totalCount = jsonObject.getString("cntOccrTot");//总期数
                var currCount = jsonObject.getString("billCnt");//当前
                var remainCount = jsonObject.getString("remainCnt");//剩余期数
                var shouldpay = jsonObject.getString("feeAmt");//本期应还
                //String remainpay = jsonObject.getString("amtCapDue");//声乐本金
                var currecy=jsonObject.getString("codCcy");
                installment.setShoppingsheetId(UUID.randomUUID().toString());
                installment.setAmountMoney(stringToBigDecimal(shouldpay));
                installment.setInstallmentType(installmentTypeChange(installment_type));
                installment.setInstallmentDesc(installment_type);
                installment.setTransDate(getDate(start_date,"yyyyMMdd"));
                installment.setTransferFee(null);
                installment.setBillId(null);
                installment.setPostDate(null);
                var currentMonth = Integer.parseInt(currCount);
                installment.setCurrentMonth(currentMonth);
                var reaminMonth=Integer.parseInt(remainCount);
                installment.setTotalMonth(reaminMonth+currentMonth);
                if (equals("01",currecy)) {
                    installment.setCurrencyType("CNY");
                }else if (currecy.equals("14")) {
                    installment.setCurrencyType("USD");
                }else {
                    installment.setCurrencyType("CNY");
                }
                installments.add(installment);
            }
            //todo 这个地方有问题
            parseInstallmentAndHandFee(billAllData, installments, handFeeList);
            billAllData.setInstallments(installments);
            logger.info("分析当前月{}账单与分期，分别对应分期和分s期手续费");
        }else{
            logger.info("当前月{}无分期数据，停止解析",billDate);
        }
    },
    crawlCreditUndoneBills:function(creditCardAllData,billAllData,creditIndex){
        var undoneBillInfo = context.get(creditIndex + "_creditUndoneBills");
        var billDays = context.get("billDays");
        var undoneBillJson = RegexUtils.GetText(undoneBillInfo, "var lQryList =*(.*)").replaceAll(";", "");
        billAllData.setBankName("农业银行");
        creditCardAllData.getBills().add(billAllData);
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("UNDONE");
        var unbillmonth = commonUtils.getUnBillDataTime(billDays);
        billAllData.setBillMonth(unbillmonth);
        //获取账单日失败的时候 赋予账单日默认值 07
        if(null == billDays || equals(billDays,"")){
            this.billDate = "07";
        }
        billAllData.setBillDate(getDate(unbillmonth + "-" + billDays,"yyyy-MM-dd"));
        billAllData.setCashLimit(creditCardAllData.getCashLimit());
        billAllData.setCreditLimit(creditCardAllData.getCreditLimit());
        billAllData.setLastPayment(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setUsdCreditLimit(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setMinPayment(null);
        if (undoneBillJson.equals("null") || undoneBillJson == null || undoneBillJson.length == 0) {
            logger.info("当前信用卡无未出账单，停止抓取未出账单信息");
            billAllData.setNewBalance(null);
            billAllData.setNewCharges(null);
            billAllData.setMinPayment(null);
            return;
        }
        var list = null;
        try {
            list = JSON.toJavaObject(JSON.parseArray(undoneBillJson), List.class);
        } catch (e) {
            logger.info("未出账单转换为jsonobject集合失败,结束抓取");
            e.printStackTrace();
            return;
        }
        logger.info("解析未出账单的信息");
        try {
            parseUndoneBillDetailOnNewEdition(billAllData, list);
        } catch (e) {
            e.printStackTrace();
            logger.info("未出账单解析失败,结束抓取，可能含有美元账单信息，错误信息为{}",e);
        }
    },
    crawlSutraBasicCardInfoList:function () {
        logger.info("ABCBank:taskKey:{},抓取借记卡列表信息",context.get(LUA_TASK_KEY));
        var basicHtmlCardList = context.get("sutra_basic_info");
        var list = new ArrayList()
        var doc = Jsoup.parse(basicHtmlCardList);
        var elements = doc.select("#tblAccountList > tbody:nth-child(1) > tr ");
        if (elements.size() > 0 && elements !== null) {
            for(var i=0;i<elements.size();i++){
                var element= elements.get(i);
                var elementss=element.select("td>input:nth-child(1)");
                if (elementss !== null && elementss.size() > 0) {
                    var basicInfo = elementss.attr("value");
                    if(basicInfo.contains("-")) {
                        logger.info("农业 taskKye= {} 当前账户{}不在抓取",context.get(LUA_TASK_KEY),basicInfo)
                        continue;
                    }
                    list.add(basicInfo);
                }
            }
        }
        context.put("basic_card_info_list",list)
    },
    crawlSutraBasicCardInfo:function () {
        logger.info("ABCBank:taskKey:{},抓取借记卡基本信息",context.get(LUA_TASK_KEY));
        var sutra_card_info = context.put(cardIndex + "sutra_card_info");

    },
    /**
     * 经典版-保存借记卡基本信息
     * @param creditCardAllData
     */
    saveSutraBasicInfo:function (creditCardAllData,basic_card_info) {
        creditCardAllData.setBankName("农业银行");
        creditCardAllData.setCardType("借记卡");
        creditCardAllData.setCardId(UUID.randomUUID().toString());
        try {
            basic_card_info = basic_card_info.split("|");
            this.setBasicInfo(basic_card_info);
        } catch (e) {
            logger.info("农业银行，经典版，任务：{} 借记卡解析基本信息失败",context.get(LUA_TASK_KEY));
            return;
        }
        // 6228480018567566270|156|0|龙枭|401|30949|北京分行|1|566270
        var fullCardNum = basic_card_info[0];
        creditCardAllData.setFullCardNum(fullCardNum);
        if (fullCardNum.length() > 4) {
            creditCardAllData.setCardNum(fullCardNum.substring(fullCardNum.length() - 4, fullCardNum.length()));//
        }
        creditCardAllData.setNameOnCard(basic_card_info[3]);
        creditCardAllData.setOpenBank(basic_card_info[6]);
        creditCardAllData.setCashBalance(null);
        creditCardAllData.setCashLimit(null);
        creditCardAllData.setCreditLimit(null);
    }
}
/**
 * 借记卡 根据获取到的 时间来获取 定期的到期时间 设置CreditCardTimeDeposit的due_date period_unit
 * period三个属性
 *
 * @param originalPeriod
 * @param deposit
 * @return
 */
function setDepositPeriod( originalPeriod,  deposit) {
    // Map<String, String>map=new HashMap<>();
    var despoitDate = deposit.getDepositDate();
    if (originalPeriod.contains("月")) {
        deposit.setPeriodUnit("MONTH");
        var index = originalPeriod.indexOf("个");
        var num = originalPeriod.substring(0, index);
        var period = Integer.parseInt(conbertArab(num));
        deposit.setPeriod(period);
        var duedate = addDueDate("MONTH", period, despoitDate);
        deposit.setDueDate(getDate(duedate,"yyyy-MM-dd"));
    }
    if (originalPeriod.contains("年")) {
        deposit.setPeriodUnit("YEAR");
        var index = originalPeriod.indexOf("年");
        var num = originalPeriod.substring(0, index);
        var period = Integer.parseInt(conbertArab(num));
        deposit.setPeriod(period);
        var duedate = this.addDueDate("YEAR", period, despoitDate);
        deposit.setDueDate(getDate(duedate,"yyyy-MM-dd"));
    }
}
function addDueDate( flag,  period,  desopoitDate) {
    var yearMonthDay = abcDateConvertString(desopoitDate,"yyyy-MM-dd").split("-");
    var due_date = "";
    if ("YEAR".equals(flag)) {
        var newYear = Integer.parseInt(yearMonthDay[0]) + period;
        due_date = newYear + "-" + yearMonthDay[1] + "-" + yearMonthDay[2];
    }
    if ("MONTH".equals(flag)) {
        var newMonth = Integer.parseInt(yearMonthDay[1]) + period;
        if (newMonth > 12) {
            newMonth = newMonth - 12;
            var newYear = Integer.parseInt(yearMonthDay[0]) + 1;
            yearMonthDay[0] = Integer.toString(newYear);
        }
        if (newMonth < 10) {
            due_date = yearMonthDay[0] + "-0" + newMonth + "-" + yearMonthDay[2];
        } else {
            due_date = yearMonthDay[0] + "-" + newMonth + "-" + yearMonthDay[2];
        }
    }
    return due_date;
}
/**
 *  将分期本金和手续费分别对应起来
 * @param billAllData
 * @param installmentList 分期本金集合
 * @param handFeeList 分期手续费集合
 */
function parseInstallmentAndHandFee( billAllData,installmentList, handFeeList){
    logger.info("分别对应{}月账单的分期分期手续费",billAllData.getBillMonth());
    bubbleSort(installmentList,"");
    bubbleSort(handFeeList, "handfee");
    if (handFeeList.size()==installmentList.size()) {
        for (var i = 0; i < installmentList.size(); i++) {
            var installment = installmentList.get(i);
            var handFee = handFeeList.get(i);
            installment.setHandingFee(handFee.getHandingFee());//
            installment.setHandingfeeDesc(handFee.getHandingfeeDesc());
        }
    }else{
        logger.info("ABCBank taskkey={}分期手续费与分期对应出错,即分期和本金并不对应，分期的数量为{}，手续费的数量为{}",context.get(LUA_TASK_KEY),installmentList.size(),handFeeList.size());
        for(var i=0;i<installmentList.size();i++){//当现在的本金和 手续费都是按照费用从大到小的顺序排序
            var installment = installmentList.get(i);
            if ((i+1)<=handFeeList.size()) {
                var handFee = handFeeList.get(i);
                installment.setHandingFee(handFee.getHandingFee());
                installment.setHandingfeeDesc(handFee.getHandingfeeDesc());
            }else{
                installment.setHandingFee(null);//如果当前分期对应的手续没有，那么手续费设置为null
                installment.setHandingfeeDesc("");
            }

        }
    }
    // billAllData.setInstallments(installmentList);
}
/**
 * 解析未出账单的信息  时尚版
 *
 * @param billAllData
 */
function parseUndoneBillDetailOnNewEdition( billAllData, list) {
    if (list != null && list.size() > 0) {
        var shopping_sheets = billAllData.getShoppingSheets();
        var order_index = shopping_sheets.size() + 1;
        for(var i=0;i<list.size();i++){
            var jsonObject = list.get(i);
            var expense = new Expense();
            var transDate = jsonObject.getString("szDATE_TR");//交易日期
            if (transDate==null) {
                transDate="";
            }
            var post_date = jsonObject.getString("szDATE_POST");//记账日期
            if (post_date==null) {
                transDate="";
            }
            var card_num = jsonObject.getString("szNUM_CARD");// 全卡号
            if (card_num==null) {
                card_num="";
            }
            var descrtipion = jsonObject.getString("szTXT_TR");//描述
            if (descrtipion==null) {
                descrtipion="";
            }
            var trans_addr = jsonObject.getString("szTXT_ADDR_TR");//交易地点
            if (trans_addr==null) {
                trans_addr="";
            }
            var amount_money = jsonObject.getString("dAMT_POST");//交易金额
            if (amount_money==null) {
                amount_money="";
            }
            var shouruFlag = jsonObject.getString("szIND_MSK_DTAL");//判断是否为收入或者支出 以此判断政府
            if (shouruFlag==null) {
                shouruFlag="";
            }
            var currecy=jsonObject.getString("szCOD_CCY_POST");//币种
            if (currecy==null) {
                currecy="";
            }
            // jsonObject.getString("");
            expense.setId(UUID.randomUUID().toString());
            expense.setTransDate(getDate(transDate,"yyyyMMdd"));
            if (card_num!=null&&card_num.length > 4) {
                expense.setCardNum(card_num.substring(card_num.length - 4, card_num.length));
            }
            expense.setTransAddr(trans_addr);
            expense.setPostDate(getDate(post_date,"yyyyMMdd"));
            //使用新的消费类型判断
            if (amount_money.contains("-")) {
                amount_money=amount_money.replaceAll("-", "");
            }else{
                amount_money="-"+amount_money;
            }
            var category=CategoryUtil.getCreditCategory("ABC", descrtipion, amount_money);
            amount_money=amount_money.replaceAll("-", "");
            expense.setCategory(category);
            expense.setOrderIndex(order_index);
            expense.setBalance(null);
            order_index++;
            expense.setDescription(descrtipion);
            if (currecy.contains("01")) {
                //amount_money = amount_money.substring(0, amount_money.length() - 3);
                expense.setCurrencyType("CNY");
            }else if (currecy.contains("14")) {
                logger.info("当前账户具有美元信息，继续抓取");
                //amount_money = amount_money.substring(0, amount_money.length() - 2);
                //这里故意写错，如果有美元账单的时候会报错
                expense.setCurrencyType("USD");
                var usd_new_balance = billAllData.getUsdNewBalance();
                if (usd_new_balance!=null) {
                    usd_new_balance.add(CreditResultUtil.StringToBigDecimal(amount_money));
                    billAllData.setUsdNewBalance(usd_new_balance);
                }else{
                    usd_new_balance=stringToBigDecimal("0");
                    usd_new_balance.add(CreditResultUtil.StringToBigDecimal(amount_money));
                    billAllData.setUsdNewBalance(usd_new_balance);
                }
                if (!descrtipion.contains("转入")){
                    var usd_new_charges = billAllData.getUsdNewCharges();
                    if(usd_new_charges != null || typeof(usd_new_charges) != "undefined" ){
                        usd_new_charges.add(CreditResultUtil.StringToBigDecimal(amount_money));
                        billAllData.setUsdNewCharges(usd_new_charges);
                    }else {
                        usd_new_charges=stringToBigDecimal("0");
                        usd_new_charges.add(CreditResultUtil.StringToBigDecimal(amount_money));
                        billAllData.setUsdNewCharges(usd_new_charges);
                    }
                }
            }
            expense.setAmountMoney(CreditResultUtil.StringToBigDecimal(amount_money));
            if (shouruFlag.length>4) {
                shouruFlag=shouruFlag.substring(shouruFlag.length-1,shouruFlag.length);
            }
            if (descrtipion.contains("转入") || descrtipion.contains("还款") || shouruFlag.equals("2")) {
                expense.setAmountMoney(expense.getAmountMoney().multiply(stringToBigDecimal("-1")));
            }else{
                //当新增的款额不为负的时候，全部想加为new_charges
                var new_charges = billAllData.getNewCharges();
                new_charges=new_charges.add(expense.getAmountMoney());
                billAllData.setNewCharges(new_charges);
            }
            //增加未出账单的new_balance
            var new_balance = billAllData.getNewBalance();
            new_balance=new_balance.add(expense.getAmountMoney());
            billAllData.setNewBalance(new_balance);

            shopping_sheets.add(expense);
        }
    } else {
        logger.info("当前账户无账单明细");
    }
}
/**
 * 根据excel的时间来判断 当前账单分属哪个月
 * @param transDate
 * @param beforeMonth
 * @param billAllData
 * @param bills
 */
function checkDetailOnMonth( transDate, beforeMonth,billAllData,bills){
    var currentMonth="";
    if (transDate !== null && transDate.length()>4) {
        currentMonth=transDate.substring(0,6);
    }
    if (equals(currentMonth,beforeMonth) || equals("",beforeMonth)) {
        billAllData.setBillDate(getDate(transDate,"yyyyMMdd"));
        billAllData.setBillMonth(getDateStr(transDate,"yyyyMMdd","yyyy-MM"));
        billAllData.setBillType("");
        billAllData.setNewBalance(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setMinPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setMinPayment(null);
        billAllData.setLastBalance(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setLastPayment(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setNewCharges(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setCreditLimit(null);
        billAllData.setCashLimit(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setUsdCreditLimit(null);
        //为避免数据结果序列化的时候 出现多引用的情况，增加如下判断：
        //1.当账单集合的大小大于0的时候，取集合中最后一个账单，取出账单月与当前账单月进行比较，如果相同证明为一个月的账单，如果不同证明为第一次进入这里，为不同月的账单，需要添加到账单集合中
        //2.当账单集合的大小等于0的时候，证明账单列表中没有账单，需要将当前账单加入到集合中
        if (bills.size()>0) {
            var beforeBill = bills.get(bills.size()-1);
            var beforeBillMonth = beforeBill.getBillMonth();
            var currenBillMonth=commonUtils.formatMonth(transDate);
            if(equals(beforeBillMonth,currenBillMonth)){

            }else{
                logger.info("当前月账单列表中没有，需再向账单列表中添加");
                bills.add(billAllData);
            }
        }else{
            bills.add(billAllData);
        }
        return billAllData;
    }else {
        logger.info("当前账单属于{}",currentMonth);
        billAllData=new BillAllData();
        billAllData.setBankName("农业银行");
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("");
        billAllData.setBillDate(getDate(commonUtils.formatMonth(transDate)+"-01","yyyy-MM-dd"));
        billAllData.setBillMonth(commonUtils.formatMonth(transDate));
        billAllData.setBillType("");
        billAllData.setNewBalance(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setMinPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setMinPayment(null);
        billAllData.setLastBalance(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setLastPayment(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setNewCharges(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setCreditLimit(null);
        billAllData.setCashLimit(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setUsdCreditLimit(null);
        bills.add(billAllData);
        //List<Expense> shopping_sheets = billAllData.getShoppingSheets();
        return billAllData;
    }
}

/**
 * 时尚版的解析借记卡excel
 * @param bills
 * @param billAllData
 * @param billList
 * @param cardId
 *
 */
function parseDepositBillExcelOnNewEdition(bills,billAllData,billList,cardId){
    var beforeMonth="";
    if (bills != null && bills.size() > 0) {
        for (var i = 0; i < bills.size(); i++) {
            if (i < 3) {
                continue;
            }
            var values = bills.get(i);
            if (values != null && values.size() > 0) {
                var expense = new Expense();
                var post_date = values.get(0).toString();
                if (post_date == null || post_date.equals("")) {
                    logger.info("当前行无数据，停止本行填充数据");
                    continue;
                }
                billAllData=this.checkDetailOnMonth(post_date, beforeMonth, billAllData, billList);
                //shopping_sheets=billAllData.getShoppingSheets();
                beforeMonth=post_date.substring(0,6);;
                post_date = getDateStr(post_date, "yyyyMMdd", "yyyy-MM-dd");;
                // var trans_date = post_date + " " + values.get(1).toString().replaceAll("(?<=^\\d{2})", ":")
                //     .replaceAll("(?<=^\\d{2}\\:\\d{2})", ":");
                var amountMoney = values.get(2).toString().trim();

                var opposite_card_no =  values.get(5);
                var name_on_opposite_card = values.get(4);
                var opposite_bank =  values.get(6);
                var trans_method =  values.get(8);
                var trans_channel =  values.get(7);
                var description =  values.get(10);
                var remake =  values.get(9);
                var balance =  values.get(3);
                expense.setId(UUID.randomUUID().toString());
                // var amount_money = Double.parseDouble(amountMoney);
                expense.setAmountMoney(stringToBigDecimal(amountMoney));
                //文档定为支出为正，收入为负，下载中支出为负，收入为正数
                expense.setAmountMoney(expense.getAmountMoney().divide(stringToBigDecimal("-1")));
                expense.setBalance(stringToBigDecimal(balance));
                var cardNum = cardId;
                expense.setCardNum(cardNum.substring(cardNum.length() - 4, cardNum.length()));
                var shopping_sheets = billAllData.getShoppingSheets();
                var index = shopping_sheets.size() + 1;
                expense.setOrderIndex(index);
                index++;
                expense.setRemark(remake);
                //使用新的判断消费类型的方法
                if (amountMoney.contains("-")) {
                    amountMoney.replaceAll("-", "");
                }else if (amountMoney.contains("+")) {
                    amountMoney="-"+amountMoney.replaceAll("\\+", "");
                }
                var category=CategoryUtil.getDebitCategory(description, amountMoney);
                if (category.equals("OTHERREVENUE")&& amountMoney.contains("-")) {
                    category=CreditResultUtil.judgeDepositBillCategory(trans_method);
                }
//					if (category.equals("OTHEREXPENSE")&&amountMoney.contains("-")) {
//						category ="OTHERREVENUE";
//					}
                if( null == category || "".equals(category)){
                    category="OTHEREXPENSE";
                }
                expense.setCategory(category);
                expense.setDescription(description);
                expense.setNameOnOppositeCard(name_on_opposite_card);
                expense.setOppositeBank(opposite_bank);
                expense.setOppositeCardNo(opposite_card_no);
                expense.setPostDate(getDate(post_date,"yyyy-MM-dd"));
                expense.setTransDate(getDate(post_date,"yyyy-MM-dd"));
                expense.setTransChannel(trans_channel);
                expense.setTransMethod(trans_method);
                // int indexOf = billDetailPage.indexOf("币种");
                // String currency_type =
                // CreditResultUtil.convertCurr(billDetailPage.substring(indexOf
                // + 3, indexOf + 6));
                expense.setCurrencyType("CNY");
                shopping_sheets.add(expense);
            }
        }
    }
}
/**
 * 根据分期的金额来排序 从小到大来排序
 * @param list
 */
function bubbleSort( list,flag){
    for(var i=0;i<list.size()-1;i++){
        for(var j=0;j<list.size()-1;j++){
            var currentBigdecimal=null;
            var nextBigdecimal=null;
            if ("".equals(flag)) {//为空的时候取本金
                currentBigdecimal=list.get(j).getAmountMoney();
                nextBigdecimal=list.get(j+1).getAmountMoney();
            }else{//不为空的时候取手续费
                currentBigdecimal=list.get(j).getHandingFee();
                nextBigdecimal=list.get(j+1).getHandingFee();//
            }
            if (nextBigdecimal.compareTo(currentBigdecimal)==1) {//当前分期的金额大于下一个
                //int temp=list.get(j);
                var tempInstallment=list.get(j);
                list.set(j, list.get(j+1));
                list.set(j+1, tempInstallment);
            }
        }
    }
}
/**
 * 借记卡 将一些基本的数据信息放入到本地Map中
 *
 * @param basicInfo
 *            6228480018567566270|156|0|龙枭|401|30949|北京分行|1|566270
 */
function setBasicInfo(basicInfo) {
    context.put("acctOpenBankId", basicInfo[5]);
    context.put("acctId", basicInfo[0]);
    context.put("acctType", basicInfo[4]);
    context.put("oofeFlg", basicInfo[2]);
    context.put("acctCurCode", basicInfo[1]);
    context.put("acctName", basicInfo[3]);
    context.put("acctOpenBankIdName", basicInfo[6]);
    context.put("random", basicInfo[8]);
}